<template>
    <div style="width: 100%; height: 100%">
        <div :style="{ maxHeight: maxHeight }" class="drawer-content">
            <Form ref="form" :model="form" :rules="formValidate" label-position="left" :label-width="100">
                <Row :gutter="32">
                    <Col span="24">
                        <FormItem label="标题" prop="newsTitle">
                            <Input v-model="form.newsTitle" clearable style="width: 100%" />
                        </FormItem>
                    </Col>
                </Row>
                <Row :gutter="32">
                    <Col span="24">
                        <FormItem label="内容" prop="newsContent">
                            <!--                <editor  v-model="form.newsContent" ref="editor" style="width:100%"-->
                            <!--                        ></editor>-->
                            <editor ref="editor" v-model="form.newsContent" height="250" maxlength="3999"></editor>

                            <!--                <Input v-model="form.newsContent" type="textarea" :rows="4" maxlength="100000" show-word-limit/>-->
                        </FormItem>
                    </Col>
                </Row>
                <Row :gutter="32">
                    <Col span="24">
                        <FormItem label="分类" prop="newsCategory">
                            <dict dict="newsType" v-model="form.newsCategory" transfer style="width: 100%" />
                        </FormItem>
                    </Col>
                </Row>
                <Row :gutter="32">
                    <Col span="24">
                        <FormItem label="关键词" prop="newsKeywords">
                            <Input v-model="form.newsKeywords" clearable style="width: 100%" />
                        </FormItem>
                    </Col>
                </Row>
                <Row :gutter="32">
                    <Col span="24">
                        <FormItem label="排序值" prop="sortOrder">
                            <InputNumber v-model="form.sortOrder" clearable style="width: 100%" />
                        </FormItem>
                    </Col>
                </Row>
                <Row :gutter="32">
                    <Col span="24">
                        <FormItem label="作者" prop="newsAuthor">
                            <Input v-model="form.newsAuthor" clearable style="width: 100%" />
                        </FormItem>
                    </Col>
                </Row>
            </Form>
        </div>
        <div class="drawer-footer br" v-show="type != '0'">
            <Button type="primary" :loading="submitLoading" @click="submit">提交</Button>
            <Button @click="$emit('clickFalse')">取消</Button>
        </div>
    </div>
</template>

<script>
// 根据你的实际请求api.js位置路径修改
import { addNews, editNews } from '@/api/news.js';
import dict from '@/views/my-components/xboot/dict';
import editor from '@/views/my-components/xboot/editor';

export default {
    name: 'addEdit',
    components: {
        dict,
        editor
    },
    props: {
        value: {
            type: Boolean,
            default: false
        },
        data: {
            type: Object
        },
        type: {
            type: String,
            default: '0'
        }
    },
    data() {
        return {
            visible: this.value,
            title: '',
            submitLoading: false,
            maxHeight: 510,
            form: {
                // 添加或编辑表单对象初始化数据
                newsTitle: '',
                newsContent: '',
                newsType: '',
                newsKeywords: '',
                sortOrder: '',
                newsAuthor: ''
            },
            // 表单验证规则
            formValidate: {
                newsTitle: [{ required: true, message: '不能为空', trigger: 'change' }],
                newsType: [{ required: true, message: '不能为空', trigger: 'change' }],
                newsKeywords: [{ required: true, message: '不能为空', trigger: 'change' }],
                newsAuthor: [{ required: true, message: '不能为空', trigger: 'change' }]
            }
        };
    },
    methods: {
        init() {},
        add() {
            this.type = '2';
            this.title = '添加';
            this.maxHeight = Number(document.documentElement.clientHeight - 121) + 'px';

            this.$refs.form.resetFields();
            delete this.form.id;
            this.$refs.editor.setData('');
        },
        edit(e) {
            this.type = '1';
            this.title = '编辑';
            this.maxHeight = Number(document.documentElement.clientHeight - 121) + 'px';

            this.$refs.form.resetFields();
            this.form = e;
            this.$refs.editor.setData(this.form.newsContent);
        },
        submit() {
            this.$refs.form.validate(valid => {
                if (valid) {
                    if (this.type == '1') {
                        // 编辑
                        this.submitLoading = true;
                        editNews(this.form).then(res => {
                            this.submitLoading = false;
                            if (res.success) {
                                this.$Message.success('操作成功');
                                this.$emit('clickFalse');
                                this.$emit('on-submit', true);

                                // this.visible = false;
                            }
                        });
                    } else {
                        // 添加
                        this.submitLoading = true;
                        addNews(this.form).then(res => {
                            this.submitLoading = false;
                            if (res.success) {
                                this.$Message.success('操作成功');
                                this.$emit('on-submit', true);
                                this.visible = false;
                            }
                        });
                    }
                }
            });
        },
        setCurrentValue(value) {
            if (value === this.visible) {
                return;
            }
            if (this.type == '1') {
            } else if (this.type == '2') {
            } else {
                this.title = '详细信息';
                this.maxHeight = '100%';
            }
            // 清空数据
            this.$refs.form.resetFields();
            if (this.type == '0' || this.type == '1') {
                // 回显数据处理
                this.form = this.data;
            } else {
                // 添加
                delete this.form.id;
            }
            this.visible = value;
        }
    },
    watch: {
        value(val) {
            this.setCurrentValue(val);
        },
        visible(value) {
            this.$emit('input', value);
        }
    },
    mounted() {
        this.init();
    }
};
</script>

<style lang="less">
// 建议引入通用样式 具体路径自行修改 可删除下面样式代码
// @import "../../../styles/drawer-common.less";
.drawer-footer {
    z-index: 10;
    width: 100%;
    position: absolute;
    bottom: 0;
    left: 0;
    border-top: 1px solid #e8e8e8;
    padding: 10px 16px;
    text-align: left;
    background: #fff;
}

.drawer-content {
    overflow: auto;
}

.drawer-content::-webkit-scrollbar {
    display: none;
}

.drawer-header {
    display: flex;
    align-items: center;
    margin-bottom: 16px;
    font-size: 16px;
    color: rgba(0, 0, 0, 0.85);
}

.drawer-title {
    font-size: 16px;
    color: rgba(0, 0, 0, 0.85);
    display: block;
    margin-bottom: 16px;
}
</style>
